package com.ruoyi.common.core.config.databind;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

/**
 * Copyright (c) 2023, gitee
 * All rights reserved.
 *
 * @author qiupt@gitee.com
 * @description jackson前端传递json对象，后端用string接收。自定义JsonDeserialize反序列化
 * @date 2023/08/09 17:07
 **/
public class JsonToStringDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jsonParser,
                              DeserializationContext deserializationContext) throws IOException {
        if (jsonParser == null) {
            return null;
        }
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        if (node == null) {
            return null;
        }
        return node.toString();
    }
}
